// 定义游戏状态
function addGame(games, newGame) {
    games.push(newGame);
    //console.log("数组长度: " + games.length);
}

//根据卡牌组打印卡牌名
function printcards(biao){
    for (let i = 0; i < biao.length; i++) {
        console.log("名字: " + biao[i]);
    }
}

//3高级5先机
//["common","super","advanced","heart","advantage","master"]
const costlist={
    "poke__yibu":{cost:[2,0,0,0,3,1],value:[3,3],score:1},
    "poke__zoulucao3":{cost:[0,0,2,0,2,0],value:3,score:0,evolve:[0,0,0,0,2,0]},
    "poke__zoulucao2":{cost:[0,3,0,0,0,0],value:3,score:0,evolve:[0,0,0,0,2,0]},
    "poke__zoulucao":{cost:[1,0,0,2,0,0],value:3,score:0,evolve:[0,0,0,0,2,0]},
    "poke__yongjila":{cost:[4,0,1,0,4,0],value:4,score:3,evolve:[0,0,4,0,0,0]},
    "poke__yongjila2":{cost:[0,0,0,6,0,0],value:4,score:1,evolve:[0,0,4,0,0,0]},
    "poke__xiaoquanshi":{cost:[1,0,1,1,1,0],value:2,score:0,evolve:[0,0,0,3,0,0]},
    "poke__xiaohuolong2":{cost:[0,4,0,0,0,0],value:2,score:1,evolve:[0,0,0,0,3,0]},
    "poke__xiaohuolong":{cost:[0,0,3,2,0,0],value:2,score:1,evolve:[0,0,0,0,3,0]},
    "poke__wenxiangyongshi":{cost:[2,0,0,5,2,0],value:4,score:3},
    "poke__wenxiangwa2":{cost:[0,2,0,3,2,0],value:4,score:1,evolve:[0,0,4,0,0,0]},
    "poke__wenxiangwa":{cost:[2,2,3,0,0,0],value:4,score:1,evolve:[0,0,4,0,0,0]},
    "poke__wenxiangkedou":{cost:[0,2,0,0,1,0],value:4,score:0,evolve:[0,0,2,0,0,0]},
    "poke__wenxiangkedou2":{cost:[3,0,0,0,0,0],value:4,score:0,evolve:[0,0,2,0,0,0]},
    "poke__wenxiangkedou3":{cost:[0,0,2,2,0,0],value:4,score:0,evolve:[0,0,0,2,0,0]},
    "poke__wanli2":{cost:[0,0,1,1,2,0],value:1,score:0,evolve:[0,0,0,0,3,0]},
    "poke__wanli":{cost:[0,1,1,1,1,0],value:1,score:0,evolve:[0,0,0,0,3,0]},
    "poke__tiekeayong":{cost:[4,2,0,1,0,0],value:3,score:2,evolve:[0,0,0,3,0,0]},
    "poke__tiekeyong2":{cost:[0,0,5,0,2,0],value:3,score:2,evolve:[0,0,0,3,0,0]},
    "poke__tiejiayong":{cost:[4,2,0,0,1,0],value:4,score:2,evolve:[0,0,0,0,3,0]},
    "poke__tiejiayong2":{cost:[0,0,2,5,0,0],value:4,score:2,evolve:[0,0,0,0,3,0]},
    "poke__shuijiangui":{cost:[0,7,3,0,0,0],value:1,score:5,evolve:[0,0,0,0,3,0]},
    "poke__shandianniao":{cost:[0,3,0,3,3,1],value:[1,1],score:2},
    "poke__pikaqiu":{cost:[0,3,0,2,0,1],value:[5,5],score:0},
    "poke__penghuolong":{cost:[0,0,7,0,3,0],value:2,score:5},
    "poke__niduona":{cost:[0,3,2,2,0,0],value:5,score:1,evolve:[0,4,0,0,0,0]},
    "poke__niduona2":{cost:[2,0,0,2,3,0],value:5,score:1,evolve:[0,4,0,0,0,0]},
    "poke__niduolan":{cost:[2,0,0,1,0,0],value:5,score:0,evolve:[0,2,0,0,0,0]}, 
    "poke__niduolan2":{cost:[0,0,3,0,0,0],value:5,score:0,evolve:[0,2,0,0,0,0]}, 
    "poke__niduolan3":{cost:[0,2,0,0,2,0],value:5,score:0,evolve:[0,2,0,0,0,0]}, 
    "poke__niduohou":{cost:[2,0,0,2,5,0],value:5,score:3},
    "poke__minilong":{cost:[2,0,0,0,3,0],value:3,score:1,evolve:[0,3,0,0,0,0]},
    "poke__minilong2":{cost:[0,0,4,0,0,0],value:3,score:1,evolve:[0,3,0,0,0,0]},
    "poke__miaowazhongzi":{cost:[3,0,2,0,0,0],value:5,score:1,evolve:[0,0,0,3,0,0]},
    "poke__miaowazhongzi2":{cost:[0,0,0,0,4,0],value:5,score:1,evolve:[0,0,0,3,0,0]},
    "poke__miaowahua":{cost:[7,0,0,3,0,0],value:5,score:5},
    "poke__miaohaucao":{cost:[4,1,0,4,0,0],value:5,score:3,evolve:[0,4,0,0,0,0]},
    "poke__miaohaucao2":{cost:[0,0,0,0,6,0],value:5,score:3,evolve:[0,4,0,0,0,0]},
    "poke__miaomiao":{cost:[0,2,3,0,0,1],value:[1,1],score:0},
    "poke__menghuan":{cost:[3,0,3,0,3,1],value:[2,2],score:2},
    "poke__meiluoyeta":{cost:[3,3,0,3,0,1],value:[3,3],score:2},
    "poke__lvmaochong":{cost:[1,1,1,0,1,0],value:4,score:0,evolve:[0,3,0,0,0,0]},
    "poke__lvmaochong2":{cost:[0,1,2,0,1,0],value:4,score:0,evolve:[0,3,0,0,0,0]},
    "poke__longlongyan":{cost:[4,0,0,6,0,0],value:2,score:4},
    "poke__longlongshi":{cost:[0,0,1,4,2,0],value:2,score:2,evolve:[0,0,3,0,0,0]},
    "poke__labaya3":{cost:[2,0,0,2,0,0],value:1,score:0,evolve:[0,0,0,2,0,0]},
    "poke__labaya2":{cost:[0,0,0,0,3,0],value:1,score:0,evolve:[0,0,0,2,0,0]},
    "poke__labaya":{cost:[0,1,2,0,0,0],value:1,score:0,evolve:[0,0,0,2,0,0]},
    "poke__kuailong":{cost:[0,3,0,7,0,0],value:3,score:5},
    "poke__labahua2":{cost:[3,0,2,0,2,0],value:1,score:1,evolve:[0,0,0,4,0,0]},
    "poke__labahua":{cost:[0,0,2,3,2,0],value:[1,1],score:1,evolve:[0,0,0,4,0,0]},
    "poke__kadaya":{cost:[3,0,2,0,0,1],value:[4,4],score:0},
    "poke__kamigui2":{cost:[6,0,0,0,0,0],value:1,score:3,evolve:[0,0,0,4,0,0]},
    "poke__kamigui":{cost:[0,4,4,1,0,0],value:1,score:3,evolve:[0,0,0,4,0,0]},
    "poke__kaixi2":{cost:[0,0,0,4,0,0],value:4,score:1,evolve:[3,0,0,0,0,0]},
    "poke__kaixi":{cost:[0,3,0,0,2,0],value:4,score:1,evolve:[3,0,0,0,0,0]},
    "poke__kailudiou":{cost:[3,0,3,0,3,1],value:[2,2],score:2},
    "poke__kabishou":{cost:[3,0,2,0,0,1],value:[4,4],score:1},
    "poke__jienigui2":{cost:[4,0,0,0,0,0],value:1,score:1,evolve:[0,0,3,0,0,0]},
    "poke__jienigui":{cost:[0,2,0,3,0,0],value:1,score:1,evolve:[0,0,3,0,0,0]},
    "poke__jielaaola":{cost:[3,0,3,3,0,1],value:[5,5],score:2},
    "poke__jidongniao":{cost:[3,0,3,3,0,1],value:[5,5],score:2},
    "poke__huoyanniao":{cost:[0,3,3,0,3,1],value:[4,4],score:2},
    "poke__huokonglong":{cost:[1,0,4,0,4,0],value:2,score:3,evolve:[4,0,0,0,0,0]},
    "poke__huokonglong2":{cost:[0,6,0,0,0,0],value:2,score:3,evolve:[4,0,0,0,0,0]},
    "poke__hudi":{cost:[3,0,0,0,7,0],value:4,score:5},
    "poke__huashiyilong":{cost:[0,3,0,2,0,1],value:[5,5],score:1},
    "poke__haoli":{cost:[0,1,0,2,4,0],value:1,score:2,evolve:[0,3,0,0,0,0]},
    "poke__haoli2":{cost:[5,0,0,2,0,0],value:1,score:2,evolve:[0,3,0,0,0,0]},
    "poke__hakelong":{cost:[0,4,0,4,1,0],value:3,score:3,evolve:[0,0,0,0,4,0]},
    "poke__hakelong2":{cost:[0,0,6,0,0,0],value:3,score:3,evolve:[0,0,0,0,4,0]},
    "poke__guoranwen":{cost:[0,0,0,3,2,1],value:[2,2],score:2},
    "poke__guisitong2":{cost:[0,2,0,0,5,0],value:5,score:2,evolve:[3,0,0,0,0,0]},
    "poke__guisitong":{cost:[1,0,4,2,0,0],value:5,score:3,evolve:[3,0,0,0,0,0]},
    "poke__guisi2":{cost:[1,0,1,2,0,0],value:5,score:0,evolve:[0,0,3,0,0,0]},
    "poke__guisi":{cost:[1,1,1,1,0,0],value:5,score:0,evolve:[0,0,3,0,0,0]},
    "poke__guaili":{cost:[0,0,0,4,6,0],value:1,score:4},
    "poke__genggui":{cost:[0,4,6,0,0,0],value:5,score:4},
    "poke__dujiaochong2":{cost:[1,2,0,1,0,0],value:3,score:0,evolve:[3,0,0,0,0,0]},
    "poke__dujiaochong":{cost:[1,1,0,1,1,0],value:3,score:0,evolve:[3,0,0,0,0,0]},
    "poke__dianxi":{cost:[0,3,3,0,3,1],value:[4,4],score:2},
    "poke__dazhenfeng":{cost:[6,0,0,0,4,0],value:3,score:4},
    "poke__dayanshe":{cost:[2,0,0,0,3,1],value:[3,3],score:1},
    "poke__dashihua":{cost:[5,2,2,0,0,0],value:1,score:3},
    "poke__chouchouhua2":{cost:[2,2,3,0,0,0],value:3,score:1,evolve:[0,0,0,0,4,0]},
    "poke__chouchouhua":{cost:[2,2,0,0,3,0],value:3,score:1,evolve:[0,0,0,0,4,0]},
    "poke__chenglong":{cost:[0,2,3,0,0,1],value:[1,1],score:0},
    "poke__chaomeng":{cost:[3,3,0,3,0,1],value:[3,3],score:2},
    "poke__bobo3":{cost:[2,2,0,0,0,0],value:2,score:0,evolve:[2,0,0,0,0,0]},
    "poke__bobo2":{cost:[0,0,0,3,0,0],value:2,score:0,evolve:[2,0,0,0,0,0]},
    "poke__bobo":{cost:[0,0,1,0,2,0],value:2,score:0,evolve:[2,0,0,0,0,0]},
    "poke__biketini":{cost:[0,3,0,3,3,1],value:[1,1],score:2},
    "poke__bidiao":{cost:[0,5,2,0,2,0],value:2,score:3},
    "poke__bibiniao2":{cost:[0,3,2,2,0,0],value:2,score:1,evolve:[4,0,0,0,0,0]},
    "poke__bibiniao":{cost:[3,0,0,2,2,0],value:2,score:1,evolve:[4,0,0,0,0,0]},
    "poke__bawanghua":{cost:[0,2,5,2,0,0],value:3,score:3},
    "poke__baibianguai":{cost:[0,0,0,3,2,1],value:[2,2],score:2},
    "poke__badadie":{cost:[0,6,4,0,0,0],value:4,score:4},



}
//

function countnum(selectedGems){
    const selectedGemsCount = selectedGems.reduce((a, b) => a + b, 0);
    //console.log(selectedGemsCount);
    return selectedGemsCount;
}

function countnumwithn(selectedGems, n){
    const selectedGemsCount =countnum(selectedGems); 
    return selectedGemsCount >= n;
}

function getcardcost(card){
    return costlist[card].cost ;
    //return  [0,0,0,0,0,0];
    
}

function updategems(selectedGems,index){
    selectedGems[index]+=1 ;
}

function setgemszero(selectedGems){
    selectedGems=[0,0,0,0,0,0] ;
}

//判断是否可以购买，大师球可做万能球即master可做万能球
function iscanbuy(tobuycard, player_gems) {
    const cost = getcardcost(tobuycard);
    let remainingCost = [...cost];
    let masterBallCount = player_gems[5];

    for (let i = 0; i < 6; i++) {
        if (player_gems[i] < remainingCost[i]) {
            let deficit = remainingCost[i] - player_gems[i];
            if (masterBallCount >= deficit) {
                masterBallCount -= deficit;
                remainingCost[i] = 0;
            } else {
                remainingCost[i] -= masterBallCount;
                masterBallCount = 0;
            }
        } else {
            remainingCost[i] = 0;
        }
    }

    return remainingCost.every(color => color === 0);
} 

// 判断是否合法的函数
function isActionValid(selectedCards, selectedGems,player_gems) {
    //const player = gameState.players[playerIndex];
    const selectedGemsCount =countnum(selectedGems);
    //console.log("selectedGemsCount: " + selectedGemsCount);

    // 1. 选择三个不同宝石
    if (selectedGemsCount=== 3 && selectedGems.filter(count => count > 0).length === 3 && selectedCards.length === 0
&& selectedGems[5]=== 0) {
        return true;
    }

    // 2. 选择两个同样宝石
    if (selectedCards.length === 0 && selectedGemsCount === 2 && selectedGems.filter(count => count === 2).length === 1 
    && selectedGems[5]=== 0) {
        return true;
    }

    // 3. 购买一张卡
    if (selectedCards.length === 1 && selectedGemsCount === 0) {
        const card = selectedCards[0];
        //const cost=getcardcost(card);
        //const totalGems = { ...player.gems, ...selectedGems };
        return iscanbuy(card, player_gems);
    }
    /*
    // 4. 预定一张卡和一张钻石（masterball）
    if (selectedCards.length === 1 && selectedGemsCount === 1 &&selectedGems[5]=== 1) {
        return true;
    }
    */
    return false;
}


function ismasterball(selectedCards, selectedGems){
    const selectedGemsCount =countnum(selectedGems);
    //return selectedGemsCount === 1 && selectedCards.length === 1 && selectedGems[5]=== 1;
    return false;
}
//获取卡牌全部信息
function getcardintro(selectedCards){
    let ret=[];
    if (selectedCards.length===0) {
        return ["chooseball"]}
    let selectedCard=selectedCards[0];

    ret[0]=selectedCard;
    ret[1]=costlist[selectedCard].cost ;  
    ret[2]=costlist[selectedCard].value;
    ret[3]=costlist[selectedCard].score;
    ret[4]=costlist[selectedCard].evolve;
    
    return ret;
}

